﻿using DG.Tweening;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;

namespace Miao.UI
{
    /// <value>
    /// 需要添加<CanvasGroup>
    /// </value>
    public class FadeUIMotion : BaseUIMotion
    {
        float duration;
        CanvasGroup canvasGroup;

        public FadeUIMotion(float duration = 0.5f)
        {
            this.duration = duration;
        }
        public override void Init()
        {
            base.Init();
            canvasGroup = TweenGameObject.GetComponent<CanvasGroup>();
#if UNITY_EDITOR
            if (canvasGroup == null)
            {
                Debug.LogError($"使用{nameof(FadeUIMotion)}请绑定{nameof(CanvasGroup)}");
                return;
            }
#endif
        }


        public override void Play(bool forward, TweenCallback complete)
        {
            float fromAlpha = forward ? 0f : 1f;
            float toAlpha = forward ? 1f : 0f;

            canvasGroup.alpha = fromAlpha;
            var tweener = canvasGroup.DOFade(toAlpha, duration);
            tweener.OnComplete(complete);
        }
    }
}
